<?php

namespace app\model;

use think\Model;
use think\model\concern\SoftDelete;

class ContentArticle extends Model
{
  use SoftDelete;
  protected $table = 'so_content_article';
  protected $autoWriteTimestamp = true;

  public function getCoverAttr($value)
  {
    if (empty($value))
      return 'https://img.soyul.cn/respicture/1/1/202011030813239163.png';
    return $value;
    // |default='__MZBLOG__/images/excerpt.jpg'
  }

  // 文章主列表
  public static function getMainList($where, $select)
  {
    $model = self::where('status', 2)->withoutField('content, content_html');

    // 条件查询当前账号的私密文章
    if (!is_null($select)) {
      $queryOrLimit = $select;
      $queryOrLimit[] = ['status', 'in', [1, 3, 4]];
      $model = $model->whereOr(function ($query) use ($queryOrLimit) {
        $query->where($queryOrLimit);
      });
    }

    $count = $model->count();
    $list = $model->order('publish_time desc')->page($where['page'], $where['limit'])->select()->toArray();

    return compact('count', 'list');
  }

  // 文章分类列表
  public static function getSubList($mainId, $where)
  {
    $query[] = ['status', '=', 2];
    $query[] = ['website_category', '=', $mainId];
    if (request()->sellerId)
      $query[] = ['seller_id', '=', request()->sellerId];
    if (request()->masterId)
      $query[] = ['master_id', '=', request()->masterId];

    return self::where($query)->withoutField('content, content_html')->order('publish_time desc')->page($where['page'], $where['limit'])->select()->toArray();
  }

  // 文章搜索列表
  public static function getSearchList($where, $select, $key)
  {
    $model = self::where('status', 2)->withoutField('content, content_html');

    // 条件查询当前账号的私密文章
    if (!is_null($select)) {
      $queryOrLimit = $select;
      $queryOrLimit[] = ['status', 'in', [1, 3, 4]];
      $model = $model->whereOr(function ($query) use ($queryOrLimit) {
        $query->where($queryOrLimit);
      });
    }

    if (!empty($key)) {
      $model = $model->where('title|summary', 'like', '%' . trim($key) . '%');
    }

    // $count = $model->count();
    $list = $model->order('publish_time desc')->select()->toArray();
    return $list;

    // return compact('count', 'list');
  }

  # 今日推荐列表
  public static function getRecommendList()
  {
    return self::where('status', 2)->withoutField('content, content_html')->limit(1)->order('pageviews desc')->select()->toArray();
  }

  # 热门文章列表
  public static function getHotList()
  {
    return self::where('status', 2)->withoutField('content, content_html')->order('pageviews desc')->limit(5)->select()->toArray();
  }

  # 根据网站专栏和文章发布时间找到上一篇文章
  public static function getPrevArticle($websiteCategory, $publishTime)
  {
    return self::where('website_category', $websiteCategory)->where('publish_time', '>', $publishTime)->order('publish_time desc')->field('akey, title')->find();
  }

  # 根据网站专栏和文章发布时间找到下一篇文章
  public static function getNextArticle($websiteCategory, $publishTime)
  {
    return self::where('website_category', $websiteCategory)->where('publish_time', '<', $publishTime)->order('publish_time desc')->field('akey, title')->find();
  }
}
